package com.xinfu.newproject.ui;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.ui.login_register.BackHandledInterface;
import com.xinfu.newproject.presenter.PhonePresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.LoginUtils;
import com.xinfu.newproject.view.IView;

import net.qiujuer.genius.widget.GeniusEditText;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class LoginFragment extends BaseFragment implements IView.IPhoneView {

    @BindView(R.id.head_text)
    TextView titleTv;

    @BindView(R.id.login_pass_edt)
    GeniusEditText phoneEt;

    @BindView(R.id.login_pass_hint)
    TextView phone_hint_tv;

    @BindView(R.id.login_look)
    ImageView phoneIv;

    @BindView(R.id.login_pass_edt_bg)
    View view;

    @BindView(R.id.login_go_login)
    Button goBt;

    private PhonePresenter mPhonePresenter = new PhonePresenter(this);
    private Disposable mDisposable; //用于Activity销毁时停止执行

    private LoginUtils loginUtils;

    private boolean mIsPasswordRight = false;

    protected BackHandledInterface mBackHandledInterface;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_login;
    }

    @Override
    protected void setUpView() {
        titleTv.setText("");
        loginUtils = new LoginUtils(getMContext());
        loginUtils.isPhoneListener(phoneEt, phone_hint_tv, phoneIv, view, new LoginUtils.EditIsPhoneListener() {

            @Override
            public void isPhoneListenerRight(boolean bool) {
                mIsPasswordRight = bool;
                Log.i("testOne", bool + "");
                isLogin();
            }
        });
    }

    @Override
    protected void setUpData() {

    }

    private void isLogin() {
        if (mIsPasswordRight) {
            goBt.setEnabled(true);
            goBt.setBackgroundResource(R.drawable.business_time_bg);
            goBt.setTextColor(getResources().getColor(R.color.colorWhite));
        } else {
            goBt.setEnabled(false);
            goBt.setBackgroundResource(R.drawable.gray_background);
            goBt.setTextColor(getResources().getColor(R.color.location_color));
        }
    }

    @OnClick({R.id.login_go_login, R.id.head_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_go_login:
                mPhonePresenter.phone();
                break;
            case R.id.head_back:
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    @Override
    public String getUserName() {
        return phoneEt.getText().toString().trim();
    }

    @Override
    public void showToast(String content) {
        BToast.showText(getMContext(), content);
    }

    @Override
    public void closeDispose(Disposable disposable) {
        this.mDisposable = disposable;
    }

    @Override
    public void showProgress() {
        ((LoginActivity) getMContext()).showLoding();
    }

    @Override
    public void hideProgress() {
        ((LoginActivity) getMContext()).hideLoding();
    }

    @Override
    public void toOtherActivity(int fragment) {
        mBackHandledInterface = (BackHandledInterface) getMContext();
        mBackHandledInterface.setSelectedFragment(fragment, phoneEt.getText().toString().trim());
    }
}
